package com.zsc.newbuds.service.impl;

import com.zsc.newbuds.consts.Consts;
import com.zsc.newbuds.entity.*;
import com.zsc.newbuds.mapper.AdminMapper;
import com.zsc.newbuds.mapper.RecordMapper;
import com.zsc.newbuds.mapper.RecycleMapper;
import com.zsc.newbuds.mapper.UserMapper;
import com.zsc.newbuds.service.RecycleService;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author D
 */
@Service
public class RecycleServiceImpl implements RecycleService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RecycleMapper recycleMapper;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<Recycle> getRecycleList() {

        List<Recycle> recycles = new ArrayList<Recycle>();
        //String key = "list:recycleList";
        recycles = recycleMapper.getRecycleList();
        System.out.println(recycles);
        Collections.sort(recycles);
        return recycles;
    }

    @Override
    public void updateState(Integer id, String state) {
        String key = "list:recycleList";
        String adminKey = "list:adminList";
        String userKey = "list:userList";
        //書本轉換為積分的轉換率
        int books = 3;
        //衣服數量轉換率
        int clothes = 5;
        //衣服重量轉換率
        double weight = 2.50;


        if(redisUtil.hasKey(key)){
            Recycle recycle = (Recycle) redisUtil.hget(key,id.toString());
            recycle.setState(state);
            System.out.println("測試"+recycle);
            //增加工作人員處理訂單數
            Admin admin = (Admin)redisUtil.hget(adminKey,recycle.getWorkId().toString());
            adminMapper.updateOderNumber(admin.getAdminId(),admin.getOderNumber()+1);
            User user = (User)redisUtil.hget(userKey,recycle.getUserId().toString());
            System.out.println("重量"+(int)recycle.getRecycleWeight()*weight);
            int integral = (int) ((int)recycle.getRecycleWeight()*weight + recycle.getClothesNumber()* clothes + recycle.getRecycleNumber() * books);
            userMapper.updateUserIntegral(user.getUserId(),user.getIntegral()+integral);
            redisUtil.hset(key,id.toString(),recycle);
            recycleMapper.updateState(id, state);
        }else {
            recycleMapper.updateState(id, state);
        }
    }

    @Resource
    private RecordMapper recordMapper;

    @Override
    public float getWeight(Integer[] recycleId) {

        List<Integer> idList = Consts.recordIdList;
        System.out.println(idList);
        float weight = 0;
        List<Integer> recycleIdList = new ArrayList<>();
        for (Integer id : idList){
            Record record  = recordMapper.getRecordById(id);
            if (recycleIdList.contains(record.getRecycleId())) {
                continue;
            }
            recycleIdList.add(record.getRecycleId());
            Recycle recycle = recycleMapper.getRecycleById(record.getRecycleId());
            System.out.println(recycle);
            weight += recycle.getRecycleWeight();
        }
        System.out.println(weight);
        return weight;
    }


}
